<?php

/*
 * 管理员表操作 admin_user
*/
class Edm_Db_Admin extends Zend_Db_Table {
    protected $_name = "admin_user";
    protected $_primary = 'admin_id';
    protected $_db;

    /* 初始化 */
    function init() {
        $this->_db = $this->getAdapter();
        parent::init();
    }

    /* 登录成功时更新信息 */
    function loginSuccess($id, $ip) {
        $data = array('last_login' => date('Y-m-d H:i:s'), 'last_ip' => $ip);
        $where = $this->_db->quoteInto('admin_id = ?',$id);
        $this->update($data,$where);
    }

    /*
     * 管理员列表
     * @param array $filter 过滤条件
     * @return array recordset 查询结果集
    */
    public function userList($filter = array()) {

        $select = $this->_db->select();
        $countQuery = $this->_db->select();
        $select->from($this->_name, array('admin_id', 'admin_name', 'email', 'add_time', 'FROM_UNIXTIME(last_login) AS last_login', 'last_ip', 'desc'));
        $countQuery->from($this->_name, array('COUNT(*)'));
        /* 关键字 */
        if (isset($filter['keywords'])) {
            $where['admin_name'] = $this->_db->quoteInto('admin_name LIKE ?', $filter['keywords'] . '%');
            $where['email'] = $this->_db->quoteInto('email = ?', $filter['keywords']);
            $select->where($where['admin_name'])->orWhere($where['email']);
            $countQuery->where($where['admin_name'])->orWhere($where['email']);
        }
        /* 获得记录总数 */
        $count = $this->_db->fetchOne($countQuery);

        /* 分页参数 */
        $select->limitPage($filter['pageNum'], $filter['pageSize']);

        $res = array('res' => $this->_db->fetchAll($select), 'recordCount' => $count);
        return $res;
    }

    /*
     * 添加管理员
     * @param array $data 插入数据：key=>value
     * @return bool sql执行结果
    */
    public function userAdd($data) {
        return $this->_db->insert($this->_name, $data);
    }
    
    /*
     * 更新管理员信息
     * @param array $data 更新数据：key=>value
     * @return bool sql执行结果
    */
    public function userUpdate($data, $admin_id) {
        $where = $this->_db->quoteInto('admin_id = ?', $admin_id);
        $res = $this->_db->update($this->_name, $data, $where);
        return $res;
    }

    /*
     * 根据用户admin_id删除
     * @param int $admin_id
     * @return bool
     */
    public function userDelete($admin_id)
    {
        $where = $this->_db->quoteInto('admin_id = ?', $admin_id);
        $res = $this->_db->delete($this->_name, $where);
        return $res;
    }

    /*
     * 根据用户id获取信息
     * @param int admin_id
     * @return rows
     */
    public function userInfo($admin_id = 0)
    {
        $where = $this->_db->quoteInto('admin_id = ?', $admin_id);
        $row = $this->fetchRow($where);
        if (!$row) {
            return NULL;
        }
        return $row->toArray();
    }

    /*
     * 检查管理员是否存在
     * @param string $data  欲检查用户名
     * @param string $validtor 验证方式
     * @return bool 存在返回true,否则返回false
    */
    public function userExists($user, $validtor = 'add')
    {
        $result = false;
        switch($validtor) {
            case 'add' :
                $sql = 'SELECT COUNT(*) FROM ' . $this->_name . ' WHERE admin_name = :admin_name';
                $result = $this->_db->fetchOne($sql, array('admin_name' => $user['admin_name']));
                break;
            case 'password' :
                $sql = 'SELECT COUNT(*) FROM ' . $this->_name . ' WHERE admin_id = :admin_id AND password =:password';
                $result = $this->_db->fetchOne($sql, array('admin_id' => $user['admin_id'], 'password' => md5($user['password'])));
                break;
        }

        return $result;
    }

}